export TOOL_EXTENSION=/home/tools/Xuantie-900-gcc-elf-newlib-x86_64-V2.10.0/bin

TARGET = bootrom

ASM_SOURCES = startup/start.s startup/core_lsu.s

C_SOURCES = drivers/clib/fputc.c \
drivers/clib/intc.c \
drivers/clib/uart.c \
drivers/clib/vtimer.c \
drivers/newlib_wrap/printf.c \
drivers/newlib_wrap/__thead_printf.c \
drivers/newlib_wrap/vasprintf.c

C_INCLUDES = -Istartup -Idrivers/clib -Isystem

include ./cfg.mk

FLAG_ABI = 
# FLAG_MARCH = -mtune=c920 
FLAG_MARCH = -mtune=c910
CPU_ARCH_FLAG_0=c910
ifeq (${CPU_ARCH_FLAG_0}, c910)
  FLAG_MARCH += -march=rv64imafdcxtheadc
  FLAG_ABI   = -mabi=lp64d
else
  FLAG_MARCH += -march=rv64imafdcvxtheadc
  FLAG_ABI   = -mabi=lp64dv
endif
CFLAGS = ${FLAG_MARCH} ${FLAG_ABI} ${C_INCLUDES}

ifeq (${CASENAME}, coremark)
  CFLAGS += -c -O3 -mtune=c910 -static -funroll-all-loops -finline-limit=500 -fgcse-sm -fno-schedule-insns --param max-rtl-if-conversion-unpredictable-cost=100 -msignedness-cmpiv -fno-code-hoisting -mno-thread-jumps1 -mno-iv-adjust-addr-cost -mno-expand-split-imm -DITERATIONS=10000
  # CFLAGS += -c -v -funroll-all-loops -finline-limit=500 -fgcse-sm -fno-schedule-insns --param max-rtl-if-conversion-unpredictable-cost=100 -fno-code-hoisting -mno-thread-jumps1 -mno-iv-adjust-addr-cost -DITERATIONS=10000 -O3 -static -mbit-translate
else
  CFLAGS += -c -O2
endif

CFLAGS += --specs=nano.specs

LINKFLAGS = -T./startup/linker.ld -nostartfiles

LINKLIBS = -lc -lgcc -lm

CC      = ${TOOL_EXTENSION}/riscv64-unknown-elf-gcc
AS      = ${TOOL_EXTENSION}/riscv64-unknown-elf-as
LD      = ${TOOL_EXTENSION}/riscv64-unknown-elf-ld
OBJDUMP = ${TOOL_EXTENSION}/riscv64-unknown-elf-objdump
OBJCOPY = ${TOOL_EXTENSION}/riscv64-unknown-elf-objcopy
SIZE    = ${TOOL_EXTENSION}/riscv64-unknown-elf-size
CONVERT = ./bin/Srec2vmem

BUILD_DIR = build

OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))


all : $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).bin $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).dis
	${SIZE} $<
	${CONVERT} ${BUILD_DIR}/${TARGET}_inst.hex ${BUILD_DIR}/inst.pat
	${CONVERT} ${BUILD_DIR}/${TARGET}_data.hex ${BUILD_DIR}/data.pat
	${CONVERT} ${BUILD_DIR}/$(TARGET).hex      ${BUILD_DIR}/case.pat

$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)
	@$(CC) -c $(CFLAGS)  $< -o $@
	@echo "    CC $<"

$(BUILD_DIR)/%.o: %.s | $(BUILD_DIR)
	@$(CC) -c $(CFLAGS)  $< -o $@
	@echo "    AS $<"

$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) | $(BUILD_DIR)
	@${CC} ${LINKFLAGS} ${LINKLIBS} ${OBJECTS} -o $@ 
	@echo "    LD $@"

$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	@${OBJCOPY} -O srec $< ${BUILD_DIR}/${TARGET}_inst.hex -j .text*  -j .rodata* -j .eh_frame*
	@${OBJCOPY} -O srec $< ${BUILD_DIR}/${TARGET}_data.hex -j .data*  -j .bss -j .COMMON
	@${OBJCOPY} -O srec $< $@ 
	
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	@$(OBJCOPY) -O binary -S $< $@
	@echo "    OBJCOPY $@"

$(BUILD_DIR)/%.dis: $(BUILD_DIR)/%.elf
	@$(OBJDUMP) -S -Mnumeric $< > $@
	@echo "    OBJDUMP $@"

$(BUILD_DIR):
	mkdir $@

clean:
	rm -rf $(BUILD_DIR)

